package com.gec.util;

import com.gec.component.HttpServletRequest;
import com.gec.component.HttpServletRequestWapper;
import com.gec.component.RequestTask;

import java.io.FileInputStream;

public class TestUtils {

    static HttpServletRequest req =
            new HttpServletRequestWapper();

    static void test1() {
        RequestTask task = new RequestTask(null, null);
        String paras = "username=andy&password=123";
        System.out.println("输入数据:" + paras);
        task.setParamsToReq(req, paras);
        req.prtParameters();
    }

    static void test2() {
        RequestTask task = new RequestTask(null, null);
        String reqLine = "GET /Login?username=andy&password=123" +
                "&email=abc@163.com";
        task.parseRequestLine(reqLine, req);
        System.out.println("以下为客户端表单提交的参数:");
        req.prtParameters();
        System.out.println("method(请求方式):" + req.getMethod());
        System.out.println("resPath(资源地址):" + req.getResPath());
    }

    static void test3() {
        RequestTask task = new RequestTask(null, null);
        String paras = "username=andy&password=123";
        String url = "/Login";

        //post 方式:
        String reqStr = packRequestStr("POST", url, paras);
        String[] part = task.splitReqStr(reqStr);

        //get 方式:
        //String reqStr = packRequestStr("GET",url+"?"+paras,"");
        //String[] part = task.splitReqStr(reqStr);

        for (String s : part) {
            System.out.println(s + "\n");
        }
    }

    static void test4() throws Exception {
        String GET = "D:\\Projects\\GET.txt";   //[OK]
        String POST = "D:\\Projects\\POST.txt";
        FileInputStream fis = new FileInputStream(POST);

        RequestTask task = new RequestTask(null, null);
        HttpServletRequest req = task.getRequestInfo(fis);

        System.out.println("以下为客户端表单提交的参数:");
        req.prtParameters();
        System.out.println("method(请求方式):" + req.getMethod());
        System.out.println("resPath(资源地址):" + req.getResPath());
    }

    static String packRequestStr(String method, String url,
                                 String body) {
        return
                method + " " + url + " HTTP/1.1\r\n" +
                        "Host: 192.168.1.1:8080\r\n" +
                        "Connection: keep-alive\r\n" +
                        "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
                        + " AppleWebKit/537.36 (KHTML, like Gecko)\r\n" +
                        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,"
                        + "image/avif,image/webp,image/apng,*/*;q=0.8\r\n" +
                        "Accept-Encoding: gzip, deflate, br\r\n" +
                        "Accept-Language: zh-CN,zh;q=0.9\r\n\r\n"

                        + body;
    }

    public static void main(String[] args)
            throws Exception {
        test4();
    }

}
